//
//  YygDetailsViewController.m
//  ShangChengMoBan
//
//  Created by 军 on 16/8/27.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "YygDetailsViewController.h"
#import "SDCycleScrollView.h"
#import "YygDetailsTableViewCell.h"
#import "OrderViewController.h"
@interface YygDetailsViewController ()<UITableViewDataSource,UITableViewDelegate,SDCycleScrollViewDelegate>

@end

@implementation YygDetailsViewController
{
     int _page;
    NSMutableArray * _arr;
    RWScrollView * _table;
    UIProgressView * _progress;
}
-(void)headRefresh{
    _page = 1;
    [self requestData:1];
}
-(void)footerRefresh{
    _page ++;
    [self requestData:0];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    [self drawNavWithTitle:@"商品详情" WithColor:[UIColor colorWithHue:0.00 saturation:0.00 brightness:1.00 alpha:1.00]];
    [self drawBackButton];
    _table = [[RWScrollView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight-64-50) style:UITableViewStyleGrouped];
    [_table registerClass:[YygDetailsTableViewCell class] forCellReuseIdentifier:@"cell"];
     _table.tableHeaderView =[self headerView];
    [_table addHeaderWithTarget:self action:@selector(headRefresh)];
    [_table headerBeginRefreshing];
    [_table addFooterWithTarget:self action:@selector(footerRefresh)];
    _table.delegate= self;
    _table.dataSource = self;
    _table.separatorStyle = UITableViewCellSelectionStyleNone;
   
    [self.view addSubview:_table];
    [self foot];
}
#pragma mark 请求用户参与信息请求
-(void)requestData:(int)first{
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
    [requestDic setObject:@"10" forKey:@"pageSize"];
    //[requestDic setObject:_firstModel.times forKey:@"times"];
    [requestDic setObject:[NSString stringWithFormat:@"%d",_page] forKey:@"pageNum"];
    [requestDic setObject:_firstModel._id forKey:@"goodsId"];
      [NetWorking NEWjieKouQingQiu:YYG_yygDetailt ANDIP:IP And:requestDic anBlock:^(NSDictionary *dic, NSError *error) {
          [_table headerEndRefreshing];
          [_table footerEndRefreshing];
        if (!error) {
            if ([dic[Code] intValue] == 200) {
                if (first == 1) {
                    _arr = [[NSMutableArray alloc]init];
                }
                [_arr addObjectsFromArray:dic[Response][@"user"]];
              
                [_table reloadData];
                if (_arr.count == 0) {
                      [[iToast makeText:@"  暂无参与记录!  "] show];
                }
            }
        }
    }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YygDetailsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
      [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    NSDictionary * dic = _arr[indexPath.row];
    NSString *name = [NSString stringWithFormat:@"%@", dic[@"consignee"]];
    name = [name stringByReplacingCharactersInRange:NSMakeRange(1, name.length-2) withString:@"*****"];
    cell.name.text = name;
    cell.person.text = [NSString stringWithFormat:@"参与%@人次",dic[@"goodsNum"]];
    cell.time.text = dic[@"createDate"];
    
    return cell;
}
//#pragma mark 转换时间
//-(NSString)time{
//    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
//    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[dic[@"shippingTime"] intValue]];
//    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
//    return confromTimespStr;
//}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

-(UIView *)headerView{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    NSArray * image = @[_firstModel.coverImage];
    SDCycleScrollView * sdcView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, KScreenWidth, 260) imageURLStringsGroup:image];
    sdcView.delegate = self;
    sdcView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    sdcView.autoScrollTimeInterval = 3;
    sdcView.dotColor = [UIColor whiteColor];
    sdcView.placeholderImage = [UIImage imageNamed:PLaceHolderImage];
    [view addSubview:sdcView];
    _progress = [[UIProgressView alloc]initWithFrame:CGRectMake(0, sdcView.bottom, KScreenWidth, 5)];
    _progress.progressImage = [UIImage imageNamed:@"jindu"];
    _progress.transform = CGAffineTransformMakeScale(1.0f, 3.0f);
    [_progress setProgress: [_firstModel.salesAmount floatValue] / [_firstModel.originalPrice floatValue]animated:YES];
    [view addSubview:_progress];
    UILabel *num = [[UILabel alloc]initWithFrame:CGRectMake(5,sdcView.bottom+ 12, 100, 20)];
    num.text = [NSString stringWithFormat:@"总需人次:%@",_firstModel.originalPrice];//总需人次
    num.font = [UIFont systemFontOfSize:12];
    num.textColor = [UIColor lightGrayColor];
    [view addSubview:num];
    UILabel *lastLable = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth-40,sdcView.bottom+ 12, 40, 20)];
    
    lastLable.text = [NSString stringWithFormat:@"%d",[_firstModel.originalPrice intValue]-[_firstModel.salesAmount intValue]];//还需人次
    lastLable.font = [UIFont systemFontOfSize:12];
    lastLable.textColor = [UIColor colorWithHue:0.09 saturation:0.41 brightness:0.99 alpha:1.00];
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth-150,sdcView.bottom+ 12, 106, 20)];
    lable.textAlignment=NSTextAlignmentRight;
    lable.text = @"开奖还需人次:";
    lable.font = [UIFont systemFontOfSize:12];
    lable.textColor = MaintextColor;
    [view addSubview:lable];
    
    //计算名称高度
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:_firstModel.name];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:3];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,_firstModel.name.length)];
    //获取计算高度
    CGSize contentSize = [_firstModel.name boundingRectWithSize:CGSizeMake(KScreenWidth-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSParagraphStyleAttributeName:paragraphStyle} context:nil].size;
    UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(10, lable.bottom+10, KScreenWidth-20, contentSize.height)];
    title.attributedText = attributedString;//商品名称
    title.textAlignment =NSTextAlignmentCenter;
    title.font = [UIFont systemFontOfSize:14];
    title.numberOfLines = 0;
    //计算简介高度
    NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc]initWithString:_firstModel.intro];
    NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle1 setLineSpacing:3];
    [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0,_firstModel.intro.length)];
    //获取计算高度
    CGSize contentSize1 = [_firstModel.intro boundingRectWithSize:CGSizeMake(KScreenWidth-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],NSParagraphStyleAttributeName:paragraphStyle1} context:nil].size;
    UILabel * content = [[UILabel alloc]initWithFrame:CGRectMake(10, title.bottom+5,KScreenWidth-20, contentSize1.height)];
    content.numberOfLines = 0;
    content . attributedText = attributedString1;//商品简介
    content.textAlignment =NSTextAlignmentCenter;
    content.font = [UIFont systemFontOfSize:13];
    content.textColor = [UIColor lightGrayColor];
    UIView * viewXian = [[UIView alloc]initWithFrame:CGRectMake(0, content.bottom+8, KScreenWidth, 5)];
    viewXian.backgroundColor =[UIColor colorWithHue:0.00 saturation:0.00 brightness:0.93 alpha:1.00];
    UILabel * jiLu = [[UILabel alloc]initWithFrame:CGRectMake(5, viewXian.bottom+ 5, 100, 20)];
    jiLu.text = @"用户参与记录";
    jiLu.font = [UIFont systemFontOfSize:14];
    jiLu.textColor = [UIColor colorWithHue:0.07 saturation:1.00 brightness:0.99 alpha:1.00];
    UIView * viewLast = [[UIView alloc]initWithFrame:CGRectMake(5, jiLu.bottom+2, jiLu.width-15, 2)];
   viewLast.backgroundColor =[UIColor colorWithHue:0.07 saturation:1.00 brightness:0.99 alpha:1.00];
    [view addSubview:lastLable];
    [view addSubview:lastLable];
    [view addSubview:title];
    [view addSubview:content];
    [view addSubview:viewXian];
    [view addSubview:jiLu];
    [view addSubview:viewLast];
    view.frame = CGRectMake(0,0, KScreenWidth, viewLast.bottom);
    return view;
}

-(void)foot{
    UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(0, KScreenHeight-50, KScreenWidth, 50)];
    [self.view addSubview:view2];
    UILabel * lable1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 45, 30)];
    lable1.text = @"金额:";
    lable1.textColor = MaintextColor;
    lable1.font = [UIFont systemFontOfSize:15];
    [view2 addSubview:lable1];
    UILabel * money = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 40, 30)];
    money.text = @"¥1.00";
    money.textColor = [UIColor colorWithHue:0.07 saturation:1.00 brightness:0.99 alpha:1.00];
   money.font = [UIFont systemFontOfSize:15];
    [view2 addSubview:money];
    UIButton * buy = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth-100, 0, 100, 50)];
    [buy setTitle:@"立即抢购" forState:UIControlStateNormal];
    [buy setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    buy.backgroundColor =  [UIColor colorWithHue:0.07 saturation:1.00 brightness:0.99 alpha:1.00];
    buy.titleLabel.font = [UIFont systemFontOfSize:15];
    [buy addTarget:self action:@selector(buy) forControlEvents:UIControlEventTouchUpInside];
    [view2 addSubview:buy];
}
//立即购买
-(void)buy{
   OrderViewController * details = [[OrderViewController alloc]init];
    details.firstModel = _firstModel;
    [self.navigationController pushViewController:details animated:YES];
}
#pragma mark 点击轮播图
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
